package com.example.portlocalforward.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.portlocalforward.controller.model.ApiResponse;
import com.example.portlocalforward.controller.model.PortLocalForwardBaseMessageRequest;
import com.example.portlocalforward.controller.model.PortLocalForwardBatchRequest;
import com.example.portlocalforward.controller.model.PortLocalForwardRequest;
import com.example.portlocalforward.controller.model.PortLocalForwardResponse;
import com.example.portlocalforward.service.PortLocalForwardService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 端口本地转发控制器
 * 
 * @author Generated
 * @version 1.0.0
 */
@Api(tags = "端口本地转发管理", description = "Port Local Forward 管理接口")
@Slf4j
@RestController
@RequestMapping("/api/port-local-forward")
public class PortLocalForwardController {

    @Autowired
    private PortLocalForwardService portLocalForwardService;

    /**
     * 获取所有端口本地转发
     */
    @ApiOperation(value = "获取所有端口本地转发", notes = "获取所有端口本地转发列表（包含SSH隧道名称和实时状态）")
    @GetMapping
    public ResponseEntity<ApiResponse<List<PortLocalForwardResponse>>> getAllPortLocalForwards() {
        List<PortLocalForwardResponse> forwards = portLocalForwardService.getAllPortLocalForwards();
        return ResponseEntity.ok(ApiResponse.success(forwards));
    }

    /**
     * 创建端口本地转发
     */
    @ApiOperation(value = "创建端口本地转发", notes = "创建新的端口本地转发配置")
    @PostMapping
    public ResponseEntity<ApiResponse<Void>> createPortLocalForward(
            @ApiParam(value = "端口本地转发配置信息", required = true) @RequestBody PortLocalForwardRequest portLocalForwardRequest) {
        portLocalForwardService.createPortLocalForward(portLocalForwardRequest);
        return ResponseEntity.ok(ApiResponse.success());
    }

    /**
     * 更新端口本地转发
     */
    @ApiOperation(value = "更新端口本地转发", notes = "更新端口本地转发,启动中无法修改")
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> updatePortLocalForward(
            @ApiParam(value = "端口本地转发ID", required = true) @PathVariable Long id,
            @ApiParam(value = "端口本地转发配置信息", required = true) @RequestBody PortLocalForwardRequest portLocalForwardRequest) {
        portLocalForwardService.updatePortLocalForward(id, portLocalForwardRequest);
        return ResponseEntity.ok(ApiResponse.success());
    }

    /**
     * 更新端口本地转发
     */
    @ApiOperation(value = "更新端口本地转发基础信息", notes = "更新端口本地转发基础信息，启动中可以修改")
    @PutMapping("/{id}/base-message")
    public ResponseEntity<ApiResponse<Void>> updatePortLocalForwardBaseMessage(
            @ApiParam(value = "端口本地转发ID", required = true) @PathVariable Long id,
            @ApiParam(value = "端口本地转发基础信息", required = true) @RequestBody PortLocalForwardBaseMessageRequest portLocalForwardBaseMessageRequest) {
        portLocalForwardService.updatePortLocalForwardBaseMessage(id, portLocalForwardBaseMessageRequest);
        return ResponseEntity.ok(ApiResponse.success());
    }

    /**
     * 删除端口本地转发
     */
    @ApiOperation(value = "删除端口本地转发", notes = "根据ID删除端口本地转发（如果正在运行，会先停止连接）")
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deletePortLocalForward(
            @ApiParam(value = "端口本地转发ID", required = true) @PathVariable Long id) {
        portLocalForwardService.deletePortLocalForward(id);
        return ResponseEntity.ok(ApiResponse.success());
    }

    /**
     * 批量启动端口本地转发
     */
    @ApiOperation(value = "批量启动端口本地转发", notes = "批量启动多个端口本地转发连接")
    @PostMapping("/batch/start")
    public ResponseEntity<ApiResponse<List<String[]>>> batchStartPortLocalForwards(
            @ApiParam(value = "批量操作请求", required = true) @RequestBody PortLocalForwardBatchRequest batchRequest) {
        List<String[]> createResultMap = portLocalForwardService.batchStartPortLocalForwards(
                batchRequest.getPortLocalForwardIds());
        return ResponseEntity.ok(ApiResponse.success(createResultMap));
    }

    /**
     * 批量停止端口本地转发
     */
    @ApiOperation(value = "批量停止端口本地转发", notes = "批量停止多个端口本地转发连接")
    @PostMapping("/batch/stop")
    public ResponseEntity<ApiResponse<Void>> batchStopPortLocalForwards(
            @ApiParam(value = "批量操作请求", required = true) @RequestBody PortLocalForwardBatchRequest batchRequest) {
        portLocalForwardService.batchStopPortLocalForwards(
                batchRequest.getPortLocalForwardIds());
        return ResponseEntity.ok(ApiResponse.success());
    }

    /**
     * 批量切换SSH隧道
     */
    @ApiOperation(value = "批量切换SSH隧道", notes = "批量切换多个端口本地转发的SSH隧道（如果正在运行，会使用新隧道重新连接）")
    @PostMapping("/batch/switch-ssh-tunnel")
    public ResponseEntity<ApiResponse<Void>> batchSwitchSshTunnel(
            @ApiParam(value = "批量切换SSH隧道请求", required = true) @RequestBody PortLocalForwardBatchRequest batchRequest) {
        if (batchRequest.getSshTunnelId() == null) {
            return ResponseEntity.ok(ApiResponse.error("SSH隧道ID不能为空"));
        }

        portLocalForwardService.batchSwitchSshTunnel(
                batchRequest.getPortLocalForwardIds(), batchRequest.getSshTunnelId());
        return ResponseEntity.ok(ApiResponse.success());
    }
}
